//
//  RegisterViewController.m
//  UI-21-豆豆豆瓣
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 Wang Hao. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "DatabaseSingleton.h"
#import "Users.h"

@interface RegisterViewController ()

@property (nonatomic, retain) RegisterView *aview;

@end

@implementation RegisterViewController
- (void)dealloc
{
    [_aview release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注册";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)] autorelease];
    
    self.aview = [[RegisterView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:self.aview];
    [_aview release];

}
-(void)rightAction:(UIBarButtonItem *)right{
    [self.aview.bText resignFirstResponder];
    [self.aview.aText resignFirstResponder];
    //注册
    
    [[DatabaseSingleton shareDatabaseSingleton] openDB];
    Users *userO = [[Users alloc] init];
    userO.nameUser = self.aview.bText.text;
    userO.passUser = self.aview.aText.text;
    Users *user1 = [[DatabaseSingleton shareDatabaseSingleton] selectUser:self.aview.bText.text];
    
    if ([userO.nameUser isEqualToString:@""] || [userO.passUser isEqualToString:@""]) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户或密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alert addAction:action];
        
    }else if([userO.nameUser isEqualToString:user1.nameUser]){
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该用户已注册" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alert addAction:action];

        
    }
    else{
        //插入数据库
        [[DatabaseSingleton shareDatabaseSingleton] insertUser:userO];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
        [alert addAction:action];

    }
    
    
    
    
    
}
    
    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
